package com.jiangSheng.mall.app.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jiangSheng.mall.app.bean.Goods;
import com.jiangSheng.mall.app.services.GoodsServices;
import com.jiangSheng.mall.app.utils.CommonPage;
import com.jiangSheng.mall.app.utils.CommonResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/goods")
@Api("商品的方法")
public class GoodsController {
	
	@Autowired
	private GoodsServices goodsServices;

	@ApiOperation("获取所有商品")
	@RequestMapping(value = "/listAll",method = RequestMethod.GET)
	public CommonResult<List<Goods>> listAll(){
		List<Goods> goodsList = goodsServices.listAll();
		return CommonResult.success(goodsList);

	}
	
	@ApiOperation(value = "分页获取商品列表")
    @RequestMapping(value = "/goodsList", method = RequestMethod.GET)
    public CommonResult<CommonPage<Goods>> goodsList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
        List<Goods> brandList = goodsServices.goodsList(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(brandList));
    }
	
	@ApiOperation("添加商品")
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public CommonResult<Integer> save(@RequestBody Goods goods){
		int flag = goodsServices.save(goods);
		if(flag>0) {
			return CommonResult.success(flag);
        }else{
            return CommonResult.failed();
        }
	}
	
	@ApiOperation("删除商品")
	@RequestMapping(value = "/move" ,method =  RequestMethod.DELETE)
	public CommonResult<Integer> move(@PathVariable("id") Integer id){
		int flag = goodsServices.move(id);
		if(flag > 0) {
			return CommonResult.success(flag);
		}else {
			return CommonResult.failed();
		}
	}
}
